import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_badge/flutter_badge.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/pages/modules/CustomRoute.dart';
import 'package:mfkb/pages/school/market/Cart.dart';
import 'package:mfkb/pages/school/market/MarketTab.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/MarketService.dart';
import 'package:toast/toast.dart';

class SchoolMarketDetail extends StatefulWidget {
  var id ;
  var name ;
  SchoolMarketDetail({Key key , @required this.id , @required this.name}):super(key:key) ;
  
  @override
  _MarketDetailState createState() => _MarketDetailState();
}

class _MarketDetailState extends State<SchoolMarketDetail> {

  List<dynamic> types = null ;
  int count = 0 ;
  double money = 0 ;

  @override
  void initState() {
    super.initState();
    MarketService().types(widget.id).then((value){
      if(value["code"] == 200){
        types = value["data"] ;
        setState(() {});
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
    MarketService().cartData().then((value){
      if(value["code"] == 200){
        count = value["data"]["count"] ;
        money = value["data"]["money"] ;
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark ,
      child: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [Color.fromARGB(255, 255, 255, 255) , Color.fromARGB(255, 246, 246, 248)] ,
              begin: Alignment.topCenter ,
              end: Alignment.bottomCenter
            )
          ),
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).padding.top ,
              ) ,
              Container(
                height: 45,
                child: Row(
                  children: <Widget>[
                    Container(
                      width: 17,
                      height: 17,
                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(19)),
                      child: GestureDetector(
                        child: Container(
                          width: 17,
                          height: 17,
                          child: Image.asset("assets/images/back.png" , fit: BoxFit.fill,),
                        ),
                        onTap: (){
                          Navigator.of(context).pop() ;
                        },
                      ),
                    ) ,
                    Expanded(
                      child: Container(
                        child: Center(
                          child: Text(widget.name , style: TextStyle(
                            color: Color.fromARGB(255, 19 , 28 , 53) ,
                            fontSize: ScreenUtil().setSp(18) ,
                            fontWeight: FontWeight.bold 
                          ),),
                        ),
                      ),
                    ) ,
                    Container(
                      height: 17,
                      width: 36,
                    )
                  ],
                ),
              ) ,
              Expanded(
                child: types == null ? Container(
                  height: ScreenUtil().setHeight(48),
                  color:Colors.white 
                ) : MarketTab(data: types,) 
              ) ,
              Container(
                height: ScreenUtil().setHeight(60),
                decoration: BoxDecoration(
                  color: Colors.white ,
                  boxShadow: <BoxShadow>[
                    BoxShadow(
                      blurRadius: 12 ,
                      color: Color.fromARGB(80 , 0, 0, 0) ,
                      offset: Offset(0 , 3)
                    )
                  ]
                ),
                child: Row(
                  children: <Widget>[
                    GestureDetector(
                      child: Container(
                        width: ScreenUtil().setHeight(30),
                        height: ScreenUtil().setHeight(30),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(30)),
                        child: Badge(
                          child: Image.asset("assets/images/gwc1.png" , fit: BoxFit.fill,),
                          number: count,
                          offsetX: -5,
                          offsetY: -5,
                        ),
                      ),
                      onTap: (){
                        Navigator.of(context).push(CustomRoute(Cart())) ;
                      },
                    ) ,
                    Expanded(
                      child: Container(
                        height: ScreenUtil().setHeight(22),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end ,
                          crossAxisAlignment: CrossAxisAlignment.end ,
                          children: <Widget>[
                            Container(
                              child: Container(
                                child: Text("合计：" , style: TextStyle(
                                  fontSize: ScreenUtil().setSp(12) ,
                                  color: Color.fromARGB(255, 19, 28, 53) ,
                                ),),
                              ),
                            ) ,
                            Container(
                              child: Container(
                                child: Text("￥" , style: TextStyle(
                                  fontSize: ScreenUtil().setSp(12) ,
                                  color: Colors.red ,
                                  fontWeight: FontWeight.bold
                                ),),
                              ),
                            ) ,
                            Container(
                              margin: EdgeInsets.only(right:ScreenUtil().setWidth(44)) ,
                              child: Container(
                                child: Text("$money" , style: TextStyle(
                                  fontSize: ScreenUtil().setSp(18) ,
                                  color: Colors.red ,
                                  fontWeight: FontWeight.bold 
                                ),),
                              ),
                            )
                          ],
                        ),
                      ),
                    ) ,
                    GestureDetector(
                      child: Container(
                        width: ScreenUtil().setWidth(100),
                        height: ScreenUtil().setHeight(40),
                        margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                        decoration: BoxDecoration(
                          color: Color.fromARGB(255, 0, 195, 206) ,
                          borderRadius: BorderRadius.all(Radius.circular(21)) ,
                        ),
                        child: Center(
                          child: Text("去结算" , style: TextStyle(
                            fontSize: ScreenUtil().setSp(14) ,
                            fontWeight: FontWeight.bold ,
                            color:Colors.white 
                          ),),
                        ),
                      ),
                      onTap: (){
                        Navigator.of(context).push(CustomRoute(Cart())) ;
                      },
                    )
                  ],
                ),
              )
            ],
          )
        ),
      ),
    );
  }
}